import os

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import QIcon

from tools.scheduler import MyScheduler
from windows.datamangement import DataMangement
import sys

class TrayIcon(QSystemTrayIcon):
    def __init__(self, parent=None):
        super(TrayIcon, self).__init__(parent)

        self._mClied = None

        self.showMenu()
        self.other()

    def showMenu(self):
        "设计托盘的菜单，这里我实现了一个二级菜单"
        self.menu = QMenu()
        self.menu1 = QMenu()
        self.showAction1 = QAction("显示消息", self, triggered=self.showM)
        self.quitAction0 = QAction("数据库管理", self, triggered=self.dbClied)
        self.showAction2 = QAction("隐藏", self, triggered=self.hideApp)
        self.quitAction = QAction("退出", self, triggered=self.quit)

        # self.menu1.addAction(self.showAction1)
        # self.menu1.addAction(self.showAction2)
        # self.menu.addMenu(self.menu1, )

        self.menu.addAction(self.quitAction0)
        self.menu.addAction(self.showAction1)
        self.menu.addAction(self.showAction2)
        self.menu.addAction(self.quitAction)

        # self.menu1.setTitle("二级菜单")
        self.setContextMenu(self.menu)

    def other(self):
        self.activated.connect(self.iconClied)
        # 设置图标
        self.setIcon(QIcon("bitbug_favicon.ico"))
        self.icon = self.MessageIcon()

    def iconClied(self, reason):
        "鼠标点击icon传递的信号会带有一个整形的值，1是表示单击右键，2是双击，3是单击左键，4是用鼠标中键点击"
        if reason == 2 or reason == 3:
            pw = self.parent()
            # 最小化
            if pw.windowState() == Qt.WindowMinimized:
                pw.showNormal()

            if pw.isVisible():
                pass
            else:
                pw.show()

                # 隐藏和显示
                # print(pw.isVisible())
                # if pw.isVisible():
                #     pass
                #     # pw.hide()
                # else:
                #     pw.show()
                #     print("pw.show")

        print(reason)

    @property
    def mClied(self):

        return self._mClied
        # self.showMessage("提示", "你点了消息", self.icon)

    @mClied.setter
    def mClied(self, func):
        self._mClied = func
        # 把鼠标点击弹出消息的信号和槽连接
        self.messageClicked.connect(self._mClied)

    def dbClied(self):
        print("DB菜单点击")
        self.another = DataMangement()
        self.another.show()

    def showM(self):

        self.showMessage("测试", "我是消息")

    def quit(self):
        MyScheduler.shutdown(wait=False)
        # #"保险起见，为了完整的退出"
        # self.setVisible(False)
        # qApp.quit()
        pid = os.getpid()
        print("pid:" + str(pid))
        print("quit")
        sys.exit(0)
        os._exit(0)

    def hideApp(self):
        pw = self.parent()
        if pw.isVisible():
            pw.hide()


class window(QMainWindow):
    def __init__(self, parent=None):
        super(window, self).__init__(parent)
        ti = TrayIcon(self)
        ti.show()


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    w = window()
    w.show()
    sys.exit(app.exec_())
